# Replication Code for "Vote Switching in Multiparty Presidential Systems: Evidence from the Argentine 
# Chamber of Deputies" (Main)

library(data.table)
library(lme4)
library(texreg)
library(jtools)
library(huxtable)
library(ggeffects)
library(ggplot2)
library(ggstance) 


# Set your working directory to appropriate location
setwd("...")

# Read data
Data <- fread("Data.csv", header = TRUE)
Data$type_legis <- factor(Data$type_legis)


# Table 2: models 
m0<- glmer(waff2 ~ type_legis + log_NEPdnv + sectoriales +
             (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m0_fav<- glmer(waff_favorable ~ type_legis + log_NEPdnv + sectoriales +
                 (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m0_desfav<- glmer(waff_desfavorable ~ type_legis + log_NEPdnv + sectoriales +
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m0_desmob<- glmer(demobilizing ~ type_legis + log_NEPdnv + sectoriales +
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))

htmlreg(list(m0,m0_fav,m0_desfav,m0_desmob), stars = c(0.001, 0.01, 0.05), file = "Table_2",
        custom.model.names = c("Total","Favorable","Unfavorable","Demobilizing"),
        caption="Explaining waffling in Argentine House (1993-2017)", caption.above=TRUE)


# Table 3: models
m1<- glmer(waff2 ~ type_legis + seniority + log_NEPdnv + log_dias * con_modif +  
             + log_dias * sectoriales + pluralidad * pos_coord1D +
             (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m2_fav<- glmer(waff_favorable ~ type_legis + seniority + log_NEPdnv + log_dias * con_modif +  
                 + log_dias * sectoriales + pluralidad * pos_coord1D +
                 (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m3_desfav<- glmer(waff_desfavorable ~ type_legis + seniority + log_NEPdnv + log_dias * con_modif +  
                    + log_dias * sectoriales + pluralidad * pos_coord1D +
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m4_desmob<- glmer(demobilizing ~ type_legis + seniority + log_NEPdnv + log_dias * con_modif +  
                    + log_dias * sectoriales + pluralidad * pos_coord1D +
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))

htmlreg(list(m1,m2_fav,m3_desfav,m4_desmob), stars = c(0.001, 0.01, 0.05), file = "Table_3",
        custom.model.names = c("Total","Favorable","Unfavorable","Demobilizing"),
        caption="Explaining waffling in Argentine House (1993-2017)", caption.above=TRUE)


# Table 4: models
m5<- glmer(waff2 ~ type_legis * sectoriales + seniority + log_NEPdnv + log_dias + 
             con_modif + pluralidad + pos_coord1D +
             (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m6_fav<- glmer(waff_favorable ~ type_legis  * sectoriales + seniority + log_NEPdnv + log_dias + 
                 con_modif + pluralidad + pos_coord1D +
                 (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m7_desfav<- glmer(waff_desfavorable ~ type_legis  * sectoriales + seniority + log_NEPdnv + log_dias + 
                    con_modif + pluralidad + pos_coord1D + 
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m8_desmob<- glmer(demobilizing ~ type_legis  * sectoriales + seniority + log_NEPdnv + log_dias + 
                    con_modif + pluralidad + pos_coord1D + 
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))

htmlreg(list(m5,m6_fav,m7_desfav,m8_desmob), stars = c(0.001, 0.01, 0.05), file = "Table_4",
        custom.model.names = c("Total","Favorable","Unfavorable","Demobilizing"),
        caption="Explaining waffling in Argentine House (1993-2017)", caption.above=TRUE)


# Table 5: models
m9<- glmer(waff2 ~ type_legis + seniority + log_NEPdnv + log_dias + sectoriales +  
             con_modif + pluralidad + pos_coord1D + 
             (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m10_fav<- glmer(waff_favorable ~ type_legis * voto_jefeblq_AF + seniority + log_NEPdnv + log_dias + sectoriales +  
                  con_modif + pluralidad + pos_coord1D + 
                  (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m11_desfav<- glmer(waff_desfavorable ~ type_legis * voto_jefeblq_NEG + seniority + log_NEPdnv + log_dias + sectoriales +  
                     con_modif + pluralidad + pos_coord1D + 
                     (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))
m12_demob<- glmer(demobilizing ~ type_legis * voto_jefeblq_DEMOB + seniority + log_NEPdnv + log_dias + sectoriales +  
                    con_modif + pluralidad + pos_coord1D + 
                    (1|congreso/obs_dictamen), data = Data, family = binomial(link="logit"))

htmlreg(list(m9,m10_fav,m11_desfav,m12_demob), stars = c(0.001, 0.01, 0.05), file = "Table_5",
        custom.model.names = c("Total","Favorable","Unfavorable","Demobilizing"),
        caption="Explaining waffling in Argentine House (1993-2017)", caption.above=TRUE)


# Figure 1
graf1 <- ggeffect(m0, terms= c("type_legis"), ci.lvl = 0.95, x.as.factor = TRUE, logical = TRUE)
plot(graf1, ci = TRUE, ci.style = "dot", colors = "gs", use.theme = TRUE, log.y = FALSE, show.x.title = FALSE) +
  scale_y_continuous(limit = c(0,.4), breaks = seq(0,.5,.05)) 


# Figure 2
ss_sectoral_time <- sim_slopes(m1, pred = sectoriales, modx = log_dias, 
                               modx.values = c(2.079442,2.7080502,3.4011974,5.1929569))
ss_sectoral_time_desfav <- sim_slopes(m3_desfav, pred = sectoriales, modx = log_dias, 
                                      modx.values = c(2.079442,2.7080502,3.4011974,5.1929569))
plot(ss_sectoral_time, ss_sectoral_time_desfav,
     ci_level = 0.95, x.label = NULL, modx.labels = c("7 days", "15 days", "30 days","180"),
     model.names = c("Total switching", "Unfavorable switching"), colors = c("black","#8c8c8c"))


